package com.example.doublepointer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 539. 最小时间差
 * 给定一个 24 小时制（小时:分钟 "HH:MM"）的时间列表，找出列表中任意两个时间的最小时间差并以分钟数表示。
 * <p>
 * 示例 1：
 * <p>
 * 输入：timePoints = ["23:59","00:00"]
 * 输出：1
 * 示例 2：
 * <p>
 * 输入：timePoints = ["00:00","23:59","00:00"]
 * 输出：0
 */
public class findMinDifference {
    public static int findMinDifference(List<String> timePoints) {
        Collections.sort(timePoints);
        int size = timePoints.size();
        int min = Integer.MAX_VALUE;
        int first = getminute(timePoints.get(0));
        int pre = first;
        for (int i = 1; i < size; i++) {
            int cur = getminute(timePoints.get(i));
            min = Math.min(min, cur - pre);
            pre = cur;
        }
        min = Math.min(min, first + 24 * 60 - pre);
        return min;
    }

    private static int getminute(String s) {
        return ((s.charAt(0) * 10 - '0') + s.charAt(1)) * 60 + (s.charAt(3) * 10 + s.charAt(4));
    }

    public static void main(String[] args) {
        System.out.println(findMinDifference(new ArrayList<String>() {{
            add("23:59");
            add("10:00");
            add("23:50");
        }}));
    }
}
